import {appConfig} from "./config";
import request from '@/common/utils/request'
import {
    serv
} from '@/common/utils/config'
export function dictService (v, dictKeys) {
    return new Promise((resolve, reject) => {
        let qryDdflds = [];
        v.dataDictLists || (v.dataDictLists = {});
        v.dataDictMap || (v.dataDictMap = {});
        for(let i in dictKeys) {
            !appConfig.dataDict[dictKeys[i]] && qryDdflds.push(dictKeys[i]);
        }
        if(qryDdflds.length > 0) {
            request(serv.sysrest + "/api/dataDict/qryDataDictByDdfldList",
                {
                    method:'get',
                    data:{data:JSON.stringify(qryDdflds)}
                }).then(function(response) {
                let data = response.data;
                if(data){
                    for(let k in data){
                        appConfig.dataDict[k] = data[k];
                        appConfig.dataDictMap[k]={};
                        for(let idx in data[k]){
                            let row=data[k][idx];
                            appConfig.dataDictMap[k][row.code]=row.name;
                        }
                    }
                    for(let i in dictKeys) {
                        v.$set(v.dataDictLists,dictKeys[i],appConfig.dataDict[dictKeys[i]]);
                        v.$set(v.dataDictMap,dictKeys[i],appConfig.dataDictMap[dictKeys[i]]);
                    }
                }
                resolve();
            });
        }else{
            for(let i in dictKeys) {
                v.$set(v.dataDictLists,dictKeys[i],appConfig.dataDict[dictKeys[i]]);
                v.$set(v.dataDictMap,dictKeys[i],appConfig.dataDictMap[dictKeys[i]]);
            }
            resolve();
        }
    })
}
